/*
 *    Copyright © OpenAtom Foundation.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.inspur.edp.bef.dtgenerator.repository.assembler;

import com.inspur.edp.bef.bizentity.GspBizEntityObject;
import com.inspur.edp.bef.dtgenerator.repository.BeRepositoryGenCtx;
import com.inspur.edp.bef.dtgenerator.repository.assembler.method.BeInitColumnInfoMethodGenerator;
import com.inspur.edp.bef.dtgenerator.repository.assembler.property.CacheConfigIDPropGenerator;
import com.inspur.edp.bef.dtgenerator.repository.assembler.property.UseDataCachePropGenerator;
import com.inspur.edp.caf.generator.property.ClassPropertyGenerator;
import com.inspur.edp.cef.generator.repository.entity.EntityAssmeblerGenerator;
import com.inspur.edp.cef.generator.repository.entity.MainEntityAssemblerGenerator;
import com.inspur.edp.cef.generator.repository.entity.methods.InitColumnInfoMethodGenerator;
import com.inspur.edp.cef.generator.repository.entity.properties.DboIDPropertyGenerator;
import com.inspur.edp.cef.generator.repository.entity.properties.IsFiscalTablePropertyGenerator;
import com.inspur.edp.cef.generator.repository.entity.properties.MainEntityTableNamePropertyGenerator;
import com.inspur.edp.das.commonmodel.IGspCommonObject;
import io.iec.edp.caf.commons.utils.SpringBeanUtils;
import io.iec.edp.caf.databaseobject.api.entity.DatabaseObjectTable;
import com.inspur.edp.lcm.databaseobject.api.DatabaseObjectServiceForWebIde;
import io.iec.edp.caf.databaseobject.api.entity.DatabaseObjectTableCore;
import io.iec.edp.caf.databaseobject.api.service.IDatabaseObjectService;
import io.iec.edp.caf.runtime.config.BeanUtil;
import java.util.ArrayList;
import org.springframework.util.StringUtils;

public class BEMainEntityAssemblerGenerator extends MainEntityAssemblerGenerator {
  private BeRepositoryGenCtx repoGenCtx;
  //private IDatabaseObjectService dboService;
  private DatabaseObjectServiceForWebIde dboService;

  private DatabaseObjectServiceForWebIde getDboService() {
    if (dboService == null) {
      dboService = SpringBeanUtils.getBean(DatabaseObjectServiceForWebIde.class);

    }
    return dboService;
  }

  public BEMainEntityAssemblerGenerator(BeRepositoryGenCtx repoGenCtx, IGspCommonObject dataType) {
    super(repoGenCtx, dataType);
    this.repoGenCtx = repoGenCtx;
  }

  private GspBizEntityObject getBeObject() {
    return (GspBizEntityObject) cmObject;
  }

  @Override
  protected EntityAssmeblerGenerator createChildEntityAssGenerator(IGspCommonObject childObject) {
    // if (childObject.IsRef == false)
    return new BEMainEntityAssemblerGenerator(repoGenCtx, childObject);
    // return new BeExtendEntityAssemblerGenerator(repoGenCtx, childObject);
  }

  @Override
  protected DboIDPropertyGenerator getDboIDGenerator() {
    return new DboIDPropertyGenerator(getBeObject().getRefObjectName());
  }

  @Override
  protected ArrayList<ClassPropertyGenerator> createPropertyGenerators() {
    ArrayList<ClassPropertyGenerator> list = super.createPropertyGenerators();
    if (list == null) {
      list = new ArrayList<ClassPropertyGenerator>();
    }
    if (getBeObject() == getBeObject().getBelongModel().getMainObject()) {
      list.add(new UseDataCachePropGenerator(getBeObject().getBelongModel().getEnableCaching()));
      list.add(
          new CacheConfigIDPropGenerator(getBeObject().getBelongModel().getCacheConfiguration()));
    }
    return list;
  }

  @Override
  protected InitColumnInfoMethodGenerator getInitColumnMethodGenerator() {
    return new BeInitColumnInfoMethodGenerator(getContext());
  }

  @Override
  protected IsFiscalTablePropertyGenerator getIsFiscalPropGenerator() {

    return new IsFiscalTablePropertyGenerator(false);
    //年度属性已经不用了
//    DatabaseObjectTableCore dbo =
//        (DatabaseObjectTableCore)
//            getDboService()
//                .getDatabaseObjectById(
//                    getContext().getRepoGenCtx().getCefGenCtx().getMetadataPath(),
//                    getBeObject().getRefObjectName());
//    if (dbo == null) {
//      throw new RuntimeException(
//          getBeObject().getCode() + "对应的DBO[" + getBeObject().getRefObjectName() + "]没有获取到");
//    }
//    return new IsFiscalTablePropertyGenerator(dbo.isFiscalTable());
  }

  @Override
  protected MainEntityTableNamePropertyGenerator getTableNamePropGenerator() {
    if(StringUtils.isEmpty(getBeObject().getRefObjectName())){
      throw new RuntimeException(getBeObject() + "的dbo(数据库对象)未生成");
    }
    DatabaseObjectTableCore dbo =
        (DatabaseObjectTableCore)
            getDboService()
                .getDatabaseObjectById(
                    getContext().getRepoGenCtx().getCefGenCtx().getMetadataPath(),
                    getBeObject().getRefObjectName());
    if(dbo == null){
      throw new RuntimeException(getBeObject() + "的dbo(数据库对象)不存在");
    }
    return new MainEntityTableNamePropertyGenerator(dbo.getCode());
  }
}
